package edu.lnu.project.timeeditapp.widget;


import com.lnu.project.timeeditapp.R;
import edu.lnu.project.timeeditapp.activites.ListViewActivity;
import edu.lnu.project.timeeditapp.activites.StartActivity;
import edu.lnu.project.timeeditapp.helper.MyHelper;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RemoteViews;

public class WeekWidgetIntentReceiver extends BroadcastReceiver {


	@Override
	public void onReceive(Context context, Intent intent) {

		MyHelper.initializeSharedPreferences(context);
		if( intent != null && MyHelper.visibilityColorPreferences.getBoolean(StartActivity.INITIALIZED, false)){
			String action = intent.getAction();

			Bundle extras = intent.getExtras();
			int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID); 

			/* Capture update button click */
			if(action.equals("edu.lnu.project.intent.action.UPDATE_WEEK")){
				updateWeekWidgetView(context, appWidgetId);
			}

			/* Capture update button click */
			else if(action.equals("edu.lnu.project.intent.action.GO_TO_LISTVIEW")){
				goToListView(context, appWidgetId);
			}
		}
	}

	private void updateWeekWidgetView(Context context, int appWidgetId) {
		RemoteViews remoteViews = WeekWidgetProvider.updateWidgetListView(context, appWidgetId);

		//REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
		remoteViews.setOnClickPendingIntent(R.id.buttonUpdateWeekWidgetView, WeekWidgetProvider.buildUpdateButtonPendingIntent(context, appWidgetId));
		remoteViews.setOnClickPendingIntent(R.id.buttonGoToListViewFromWidget, WeekWidgetProvider.buildListViewButtonPendingIntent(context, appWidgetId));


		WeekWidgetProvider.pushWidgetUpdate(context.getApplicationContext(), remoteViews);
	}

	private void goToListView(Context context, int appWidgetId){

		/* Start ListView Activity */
		Intent startListViewActivity = new Intent(context, ListViewActivity.class);
		startListViewActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(startListViewActivity);

	}
}
